package com.etoak.common.redis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 */
@Component
public class RedisService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * string 类型 setex命令
     */
    public void setex(String key, String value, long expire, TimeUnit timeUnit){
        stringRedisTemplate.opsForValue().set(key, value, expire, timeUnit);
    }


    /**
     * string 类型 get命令
     */
    public String get(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * hash hset
     */
    public void hset(String key,String field,String vlaue){
        stringRedisTemplate.opsForHash().put(key, field, vlaue);
    }

    /**
     * hvals
     */
    public List<String> hvals(String key){
        return stringRedisTemplate.opsForHash().values(key)
                .stream().map(item->item.toString()).toList();
    }

    /**
     * hdel
     *
     */
    public void hdel(String key,String... field){
        stringRedisTemplate.opsForHash().delete(key, field);
    }
}
